@namespace Oqtane.Modules.Admin.Sites
@using Oqtane.Interfaces
@using System.Text.RegularExpressions
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject ITenantService TenantService
@inject IAliasService AliasService
@inject ISiteService SiteService
@inject IThemeService  ThemeService
@inject ISiteTemplateService SiteTemplateService
@inject IUserService UserService
@inject IInstallationService InstallationService
@inject IDatabaseService DatabaseService
@inject IStringLocalizer<Add> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

@if (_tenants == null)
{
    <p><em>@SharedLocalizer["Loading"]</em></p>
}
else
{
    <form @ref="form" class="@(validated ? "was-validated" : "needs-validation")" novalidate>
        <div class="container">
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="name" HelpText="Enter the name of the site" ResourceKey="Name">Site Name: </Label>
                <div class="col-sm-9">
                    <input id="name" class="form-control" @bind="@_name" maxlength="200" required />
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="alias" HelpText="The urls for the site (comman delimited). This can include domain names (ie. domain.com), subdomains (ie. sub.domain.com) or virtual folders (ie. domain.com/folder)." ResourceKey="Aliases">Urls: </Label>
                <div class="col-sm-9">
                    <textarea id="alias" class="form-control" @bind="@_urls" rows="3" required></textarea>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="defaultTheme" HelpText="Select the default theme for the website" ResourceKey="DefaultTheme">Default Theme: </Label>
                <div class="col-sm-9">
                    <select id="defaultTheme" class="form-select" value="@_themetype" @onchange="(e => ThemeChanged(e))" required>
                        <option value="-">&lt;@Localizer["Theme.Select"]&gt;</option>
                        @foreach (var theme in _themes)
                        {
                            <option value="@theme.TypeName">@theme.Name</option>
                        }
                    </select>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="defaultContainer" HelpText="Select the default container for the site" ResourceKey="DefaultContainer">Default Container: </Label>
                <div class="col-sm-9">
                    <select id="defaultContainer" class="form-select" @bind="@_containertype" required>
                        <option value="-">&lt;@Localizer["Container.Select"]&gt;</option>
                        @foreach (var container in _containers)
                        {
                            <option value="@container.TypeName">@container.Name</option>
                        }
                    </select>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="siteTemplate" HelpText="Select the site template" ResourceKey="SiteTemplate">Site Template: </Label>
                <div class="col-sm-9">
                    <select id="siteTemplate" class="form-select" @bind="@_sitetemplatetype" required>
                        <option value="-">&lt;@Localizer["SiteTemplate.Select"]&gt;</option>
                        @foreach (SiteTemplate siteTemplate in _siteTemplates)
                        {
                            <option value="@siteTemplate.TypeName">@siteTemplate.Name</option>
                        }
                    </select>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="runtime" HelpText="The runtime hosting model" ResourceKey="Runtime">Runtime: </Label>
                <div class="col-sm-9">
                    <select id="runtime" class="form-select" @bind="@_runtime" required>
                        <option value="Server">@SharedLocalizer["BlazorServer"]</option>
                        <option value="WebAssembly">@SharedLocalizer["BlazorWebAssembly"]</option>
						<option value="Hybrid">@SharedLocalizer["BlazorHybrid"]</option>
					</select>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="prerender" HelpText="Specifies if the site should be prerendered (for search crawlers, etc...)" ResourceKey="Prerender">Prerender? </Label>
                <div class="col-sm-9">
                    <select id="prerender" class="form-select" @bind="@_prerender" required>
                        <option value="Prerendered">@SharedLocalizer["Yes"]</option>
                        <option value="">@SharedLocalizer["No"]</option>
                    </select>
                </div>
            </div>
            <div class="row mb-1 align-items-center">
                <Label Class="col-sm-3" For="tenant" HelpText="Select the database for the site" ResourceKey="Tenant">Database: </Label>
                <div class="col-sm-9">
                    <select id="tenant" class="form-select" value="@_tenantid" @onchange="(e => TenantChanged(e))" required>
                        <option value="-">&lt;@Localizer["Tenant.Select"]&gt;</option>
                        <option value="+">&lt;@Localizer["Tenant.Add"]&gt;</option>
                        @foreach (Tenant tenant in _tenants)
                        {
                            <option value="@tenant.TenantId">@tenant.Name</option>
                        }
                    </select>
                </div>
            </div>
            @if (_tenantid == "+")
            {
                <div class="row mb-1 align-items-center">
                    <hr class="app-rule" />
                </div>
                <div class="row mb-1 align-items-center">
                    <Label Class="col-sm-3" For="name" HelpText="Enter the name for the database" ResourceKey="TenantName">Name: </Label>
                    <div class="col-sm-9">
                        <input id="name" class="form-control" @bind="@_tenantName" maxlength="100" required />
                    </div>
                </div>
                <div class="row mb-1 align-items-center">
                    <Label Class="col-sm-3" For="databaseType" HelpText="Select the database type" ResourceKey="DatabaseType">Type: </Label>
                    <div class="col-sm-9">
                        @if (_databases != null)
						{
							<div class="input-group">
								<select id="databaseType" class="form-select" value="@_databaseName" @onchange="(e => DatabaseChanged(e))" required>
									@foreach (var database in _databases)
									{
										<option value="@database.Name">@Localizer[@database.Name]</option>
									}
								</select>
								@if (!_showConnectionString)
								{
									<button type="button" class="btn btn-secondary" @onclick="ToggleConnectionString">@Localizer["EnterConnectionString"]</button>
								}
								else
								{
									<button type="button" class="btn btn-secondary" @onclick="ToggleConnectionString">@Localizer["EnterConnectionParameters"]</button>
								}
							</div>
						}
					</div>
                </div>
				@if (!_showConnectionString)
				{
					if (_databaseConfigType != null)
					{
						@DatabaseConfigComponent
					}
				}
				else
				{
					<div class="row mb-1 align-items-center">
						<Label Class="col-sm-3" For="connectionstring" HelpText="Enter a complete connection string including all parameters and delimiters" ResourceKey="ConnectionString">Settings:</Label>
						<div class="col-sm-9">
							<textarea id="connectionstring" class="form-control" @bind="@_connectionString" rows="3"></textarea>
						</div>
					</div>
				}
                <div class="row mb-1 align-items-center">
                    <Label Class="col-sm-3" For="hostUsername" HelpText="Enter the username of an existing host user" ResourceKey="HostUsername">Host Username:</Label>
                    <div class="col-sm-9">
                        <input id="hostUsername" class="form-control" @bind="@_hostusername" required />
                    </div>
                </div>
                <div class="row mb-1 align-items-center">
                    <Label Class="col-sm-3" For="hostPassword" HelpText="Enter the password of an existing host user" ResourceKey="HostPassword">Host Password:</Label>
                    <div class="col-sm-9">
                        <input id="hostPassword" type="password" class="form-control" @bind="@_hostpassword" autocomplete="new-password" required />
                    </div>
                </div>
            }
        </div>
        <br />
        <br />
        <button type="button" class="btn btn-success" @onclick="SaveSite">@SharedLocalizer["Save"]</button>
        <NavLink class="btn btn-secondary" href="@NavigateUrl()">@SharedLocalizer["Cancel"]</NavLink>
    </form>
}

@code {
    private List<Database> _databases;
    private ElementReference form;
    private bool validated = false;
    private string _databaseName;
    private Type _databaseConfigType;
    private object _databaseConfig;
    private RenderFragment DatabaseConfigComponent { get; set; }
    private bool _showConnectionString = false;
    private string _connectionString = string.Empty;

    private List<Theme> _themeList;
    private List<ThemeControl> _themes = new List<ThemeControl>();
    private List<ThemeControl> _containers = new List<ThemeControl>();
    private List<SiteTemplate> _siteTemplates;
    private List<Tenant> _tenants;
    private string _tenantid = "-";

    private string _tenantName = string.Empty;

    private string _hostusername = string.Empty;
    private string _hostpassword = string.Empty;

    private string _name = string.Empty;
    private string _urls = string.Empty;
    private string _themetype = "-";
    private string _containertype = "-";
    private string _sitetemplatetype = "-";
    private string _runtime = "Server";
    private string _prerender = "Prerendered";

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        _tenants = await TenantService.GetTenantsAsync();
        if (_tenants.Any(item => item.Name == TenantNames.Master))
        {
            _tenantid = _tenants.First(item => item.Name == TenantNames.Master).TenantId.ToString();
        }
        _urls = PageState.Alias.Name;
        _themeList = await ThemeService.GetThemesAsync();
        _themes = ThemeService.GetThemeControls(_themeList);
        if (_themes.Any(item => item.TypeName == Constants.DefaultTheme))
        {
            _themetype = Constants.DefaultTheme;
            _containers = ThemeService.GetContainerControls(_themeList, _themetype);
            _containertype = _containers.First().TypeName;
        }
        _siteTemplates = await SiteTemplateService.GetSiteTemplatesAsync();
        if (_siteTemplates.Any(item => item.TypeName == Constants.DefaultSiteTemplate))
        {
            _sitetemplatetype = Constants.DefaultSiteTemplate;
        }

		_databases = await DatabaseService.GetDatabasesAsync();
		if (_databases.Exists(item => item.IsDefault))
		{
			_databaseName = _databases.Find(item => item.IsDefault).Name;
		}
		else
		{
			_databaseName = "LocalDB";
		}
		LoadDatabaseConfigComponent();
	}

	private void DatabaseChanged(ChangeEventArgs eventArgs)
	{
		try
		{
			_databaseName = (string)eventArgs.Value;
			_showConnectionString = false;
			LoadDatabaseConfigComponent();
		}
		catch
		{
			AddModuleMessage(Localizer["Error.Database.LoadConfig"], MessageType.Error);
		}
	}

	private void LoadDatabaseConfigComponent()
	{
		var database = _databases.SingleOrDefault(d => d.Name == _databaseName);
		if (database != null)
		{
			_databaseConfigType = Type.GetType(database.ControlType);
			DatabaseConfigComponent = builder =>
			{
				builder.OpenComponent(0, _databaseConfigType);
				builder.AddComponentReferenceCapture(1, inst => { _databaseConfig = Convert.ChangeType(inst, _databaseConfigType); });
				builder.CloseComponent();
			};
		}
	}

	private void TenantChanged(ChangeEventArgs e)
	{
		_tenantid = (string)e.Value;
		if (string.IsNullOrEmpty(_tenantName))
		{
			_tenantName = _name;
		}
		StateHasChanged();
	}

	private async void ThemeChanged(ChangeEventArgs e)
	{
		try
		{
			_themetype = (string)e.Value;
			if (_themetype != "-")
			{
				_containers = ThemeService.GetContainerControls(_themeList, _themetype);
                _containertype = _containers.First().TypeName;
            }
			else
			{
				_containers = new List<ThemeControl>();
                _containertype = "-";
            }
			StateHasChanged();
		}
		catch (Exception ex)
		{
			await logger.LogError(ex, "Error Loading Containers For Theme {ThemeType} {Error}", _themetype, ex.Message);
			AddModuleMessage(Localizer["Error.Theme.LoadContainers"], MessageType.Error);
		}
	}

	private async Task SaveSite()
	{
		validated = true;
		var interop = new Interop(JSRuntime);
		if (await interop.FormValid(form))
		{
			if (_tenantid != "-" && _name != string.Empty && _urls != string.Empty && _themetype != "-" && _containertype != "-" && _sitetemplatetype != "-")
			{
				_urls = Regex.Replace(_urls, @"\r\n?|\n", ",");
				var duplicates = new List<string>();
				var aliases = await AliasService.GetAliasesAsync();
				foreach (string name in _urls.Split(',', StringSplitOptions.RemoveEmptyEntries))
				{
					if (aliases.Exists(item => item.Name == name))
					{
						duplicates.Add(name);
					}
				}

				if (duplicates.Count == 0)
				{
					InstallConfig config = new InstallConfig();

					if (_tenantid == "+")
					{
						if (!string.IsNullOrEmpty(_tenantName) && !_tenants.Exists(item => item.Name == _tenantName))
						{
							// validate host credentials
							var user = new User();
							user.SiteId = PageState.Site.SiteId;
							user.Username = _hostusername;
							user.Password = _hostpassword;
							user.LastIPAddress = PageState.RemoteIPAddress;
							user = await UserService.LoginUserAsync(user, false, false);
                            if (user.IsAuthenticated)
                            {
								var database = _databases.SingleOrDefault(d => d.Name == _databaseName);
								var connectionString = String.Empty;
								if (_showConnectionString)
								{
									connectionString = _connectionString;
								}
								else
								{
									if (_databaseConfig is IDatabaseConfigControl databaseConfigControl)
									{
										connectionString = databaseConfigControl.GetConnectionString();
									}
								}

                                if (connectionString != "")
                                {
                                    config.TenantName = _tenantName;
                                    config.DatabaseType = database.DBType;
                                    config.ConnectionString = connectionString;
									config.HostUsername = _hostusername;
                                    config.HostPassword = _hostpassword;
                                    config.HostEmail = user.Email;
                                    config.HostName = user.DisplayName;
                                    config.IsNewTenant = true;
                                }
                                else
                                {
                                    AddModuleMessage(Localizer["Error.Required.ServerDatabase"], MessageType.Error);
                                }
                            }
                            else
                            {
                                AddModuleMessage(Localizer["Error.InvalidPassword"], MessageType.Error);
                            }
                        }
                        else
                        {
                            AddModuleMessage(Localizer["Error.TenantName.Exists"], MessageType.Error);
                        }
                    }
                    else
                    {
                        var tenant = _tenants.FirstOrDefault(item => item.TenantId == int.Parse(_tenantid));
                        if (tenant != null)
                        {
                            config.TenantName = tenant.Name;
                            config.DatabaseType = tenant.DBType;
                            config.ConnectionString = tenant.DBConnectionString;
                            config.IsNewTenant = false;
                        }
                    }

                    if (!string.IsNullOrEmpty(config.TenantName))
                    {
                        config.SiteName = _name;
                        config.Aliases = _urls;
                        config.DefaultTheme = _themetype;
                        config.DefaultContainer = _containertype;
                        config.DefaultAdminContainer = "";
                        config.SiteTemplate = _sitetemplatetype;
                        config.Runtime = _runtime;
                        config.RenderMode = _runtime + _prerender;

                        ShowProgressIndicator();

                        var installation = await InstallationService.Install(config);
                        if (installation.Success)
                        {
                            var aliasname = config.Aliases.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0];
                            NavigationManager.NavigateTo(PageState.Uri.Scheme + "://" + aliasname, true);
                        }
                        else
                        {
                            await logger.LogError("Error Creating Site {Error}", installation.Message);
                            AddModuleMessage(installation.Message, MessageType.Error);
                        }
                    }
                }
                else
                {
                    AddModuleMessage(string.Format(Localizer["Message.SiteName.InUse"], string.Join(", ", duplicates.ToArray())), MessageType.Warning);
                }
            }
            else
            {
                AddModuleMessage(Localizer["Message.Required.Tenant"], MessageType.Warning);
            }
        }
        else
        {
            AddModuleMessage(SharedLocalizer["Message.InfoRequired"], MessageType.Warning);
        }
    }

	private void ToggleConnectionString()
	{
		if (_databaseConfig is IDatabaseConfigControl databaseConfigControl)
		{
			_connectionString = databaseConfigControl.GetConnectionString();
		}
		_showConnectionString = !_showConnectionString;
	}
}
